<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>canvas</title>
    <style>
      #canvas {
        background-color: gray;
      }
    </style>
  </head>
  <body>
    <!-- 1. 定义canvas标签，必须指定宽高 -->
    <canvas id="canvas" width="800" height="400"></canvas>
    <script>
      // 2. 获取canvas的画笔
      const canvas = document.getElementById("canvas");
      const ctx = canvas.getContext("2d");
      // console.log(ctx);
      // 3. 绘画
      // 划线
      ctx.beginPath();
      ctx.strokeStyle = "yellowgreen";
      ctx.moveTo(100, 100);
      ctx.lineTo(200, 200);
      ctx.lineTo(300, 100);
      ctx.lineTo(100, 100);
      ctx.stroke(); // 闭合
      ctx.closePath();

      // 画矩形
      // ctx.strokeStyle = "red";
      // strokeRect(x, y, width, height)
      // ctx.strokeRect(100, 250, 100, 100);
      ctx.fillStyle = "red";
      ctx.fillRect(100, 250, 100, 100);

      // 画圆
      ctx.beginPath();
      ctx.arc(400, 100, 50, 0, 2 * Math.PI, true);
      // ctx.stroke();
      ctx.fill();
      ctx.closePath();

      ctx.font = "22px 宋体";
      ctx.fillText("尚硅谷", 400, 200);
    </script>
  </body>
</html>
